﻿<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link href="/style/rightdirectory.css" rel="stylesheet" />

    <script src="/script/config.js"></script>
  
</head>

<body>
    <h5>供货商信息</h5>
    <div class="form-body">
        <div class="form-horizontal  hide" id="form">

        </div>
    </div>
</body>
</html>
<script type="text/html" id="tp-form">
    <div class="form-group">
        <label class="col-sm-2">真实姓名</label>
        <div class="col-md-6 col-sm-10">
            <input type="text" class="form-control" data-form="Name" placeholder="真实姓名" value="{{Name}}" maxlength="7">
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2">电话</label>
        <div class="col-md-6 col-sm-10">
            <input type="text" class="form-control" data-form="Phone" placeholder="电话" value="{{Phone}}" maxlength="11">
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2">别名</label>
        <div class="col-md-6 col-sm-10">
            <input type="text" class="form-control" data-form="Alias" placeholder="别名" value="{{Alias}}" maxlength="18">
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2">省/市/区县</label>
        <div class="col-md-6 col-sm-10">
            <input type="text" class="form-control linkage" placeholder="省/市/区县" data-form="Provinces"  value="{{Provinces}}"  readonly="readonly">
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2">详细地址</label>
        <div class="col-md-6 col-sm-10">
            <textarea type="text" class="form-control  add" data-form="Address " placeholder="详细地址" value="{{Address}}"></textarea>
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2">编号</label>
        <div class="col-md-1 col-sm-10">
            <input id="serial" type="text" class="form-control" data-form="No" placeholder="编号" value="{{No}}" maxlength="9">
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-1 col-sm-10">
            <input type="hidden" id="Province" data-form="Province" value="{{Province}}" />
            <input type="hidden" id="City" data-form="City" value="{{City}}" />
            <input type="hidden" id="County" data-form="County" value="{{County}}" />
        </div>
    </div>

    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="button" id="btn-submit" class="btn btn-primary">保存</button>
            <button type="button" class="btn" onclick="window.history.go(-1)">退出</button>
        </div>
    </div>
</script>
<link href="/script/valid/css/tr.css" rel="stylesheet" /><!--判断如：PlatformName格式-->
<link href="/script/plugins/area/css/area.css" rel="stylesheet" />
<script src="/script/valid/form-valid.js?v=2018"></script>
<script src="/script/plugins/area/area.js"></script>
<script src="/script/plugins/area/areajson.js"></script>
<script>
    $(function () {
        var id = QueryPageUrlString("v");//判断是否是编辑进来的
        id = parseInt(id)
        var formValidEvent;
        var formValidInit = function () {
            var formValidData = [
                {
                    key: "Name",
                    datatype: "*",
                    nullmsg: "不能为空哦",
                    errormsg: "不能为空哦"
                },
                {
                    key: "Phone",
                    datatype: "m",
                    nullmsg: "不能为空哦",
                    errormsg: "请正确输入！"
                },
                {
                    key: "Alias",
                    datatype: "*",
                    nullmsg: "不能为空哦",
                    errormsg: "不能为空哦"
                },
                {
                    key: "Address",
                    datatype: "*",
                    nullmsg: "不能为空哦",
                    errormsg: "不能为空哦"
                },
                {
                    key: "Provinces",
                    datatype: "*",
                    nullmsg: "不能为空哦",
                    errormsg: "不能为空哦"
                },
            ];
            formValidEvent = $(".form-body").formValid(formValidData);
        }
        var inintData = {
            Name: "",
            Phone: "",
            Alias: "",
            No: "",
            Address: "",
            Province: "",
            City: "",
            County: "",
            Provinces:""
        };

        if (id > 0) { //存在值的情况
            $.http.json("supplier/find", {   //查询
                Id: id
            }, {
                    pass: function (data) {
                        
                        $("#form").templateRender({ el: "tp-form", data: data });//渲染模板，要先渲染才能获值
                        $(".linkage").val(data.Province + "/" + data.City + "/" + data.County);
                        $(".add").text(data.Address);
                        $(".linkage").focus(function () {//三级联动
                            $(".linkage").areaPick({
                                level: 3,
                                split: '/',
                                change: function (d) {
                                    console.log(d)
                                    var s = d.all.name.split("/")
                                    $("#serial").val(d.last.code);
                                    $("#Province").val(s[0]);
                                    $("#City").val(s[1]);
                                    $("#County").val(s[2]);
                                }
                            })

                        })
                        formValidInit();
                    },
                    error: function (d) {
                        console.log(d);
                    }
                })
        } else {
            $("#form").templateRender({ el: "tp-form", data: inintData });//渲染模板，要先渲染才能获值
            formValidInit();
        }
        $(".linkage").focus(function () {//三级联动
            $(".linkage").areaPick({  
                level: 3,
                split: '/',
                change: function (d) {
                    
                    var s = d.all.name.split("/")
                    $("#serial").val(d.last.code);
                    $("#Province").val(s[0]);
                    $("#City").val(s[1]);
                    $("#County").val(s[2]);
                }
            })

        })
        

        $("#form").on("click", "#btn-submit", function () { //保存按钮有添加和编辑
            if (!formValidEvent.check()) { return; }
            var apiUrl = "supplier/insert";
            if (id > 0) {
                apiUrl = "supplier/upd";
            }
            $("#form").ajaxFormSubmit(apiUrl, {
                pass: function (d) {
                    $.msgSucceed(d.Errmsg);
                    return
                },
                error: function (d) {
                    console.log(d)
                    $.msgFall(d.Errmsg);
                }
            }, { addData: { Id: id } })
            return
        });
    });

</script>
